package com.module.codeManage.setcode.manager;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Session;

import base.dao.GenericHibernateDao;
import base.db.HibernateSessionFactory;
import base.manager.GenericHibernateManager;

import com.module.codeBind.vo.BatchCode;
import com.module.codeBind.vo.BatchCodeItem;
import com.module.codeManage.setcode.dao.FirstCodeDao;
import com.module.codeManage.setcode.vo.FirstCode;
import com.module.codeManage.setcode.vo.FirstCodeItem;



public class FirstCodeManager extends GenericHibernateManager<FirstCode, Integer>{
   private static 	FirstCodeManager service;
   FirstCodeManager(){
	   
   }
   FirstCodeDao dao = FirstCodeDao.getInstance();

   public synchronized static FirstCodeManager getInstance(){
	   if(service==null){
		   service = new FirstCodeManager();
	   }
	   return service;
   }
   /**
    * 根据码号,查询第一层码信息
    * @param code
    * @return
    * @throws Exception
    */
    public FirstCode checkCode(String code)throws Exception{
    	FirstCode fc = new FirstCode();
    	Session session  = HibernateSessionFactory.getSession();
    	//根据传入的码去查询数据
    	fc=dao.selectInfoByCode(session,code);
    	if(fc.getCodeInfo()==null||"".equals(fc.getCodeInfo())){
    		fc.setCodeInfo(code);
    	}
    	return fc;
    }
    /**
     * 查询所有未发货的卷号
     * @param orgId
     * @param batchCodeId
     * @return
     * @throws Exception
     */
    public List<String> findRollList(String orgId,String batchCodeId)throws Exception{
    	Session session  = HibernateSessionFactory.getSession();
    	List<String> list  = new ArrayList<String>();
    	try {
			list = dao.findRollList(session,orgId,batchCodeId);
		} catch (Exception e) {
			throw e;
		}finally{
			HibernateSessionFactory.closeSession();
		}
    	return list;
    }
    /**
	 * 验证标签的有效性
	 * @param bean
	 * @return
	 */
    public Map<String ,Object> checkTag(BatchCode batchCode){
    	List<BatchCodeItem> list = batchCode.getItems();
		List<FirstCodeItem> itemList = new ArrayList();
		for(BatchCodeItem item:list){
			FirstCodeItem firstCodeItem = new FirstCodeItem();
			firstCodeItem.setRollNum(item.getVolumeNo());
			firstCodeItem.setBeginCode(item.getStartVolumeCode());
			firstCodeItem.setEndCode(item.getEndVolumeCode());
			itemList.add(firstCodeItem);			
		}
		FirstCode firstCode = new FirstCode();
		firstCode.setItems(itemList);
		Map<String,Object> rmap = new HashMap<String,Object>(); 
		rmap.put("firstCode", firstCode); 
		rmap.put("flag", true);//dengsq true是贴码处理的验证；false 是码出库的验证
		
    	Session session  = HibernateSessionFactory.getSession();
    	Map<String ,Object> resultmap = new HashMap<String, Object>();//返回结果集合Map<String ,Object> resultmap = new HashMap<String, Object>();//返回结果集合
    	try {
    		resultmap=dao.checkTag(session,rmap);
		} catch (Exception e) {
		}finally{
			HibernateSessionFactory.closeSession();
		}
    	return resultmap;
    }
   /**
    * 更新标签
    * @param bean
    * @return
    * @throws Exception
    */
    public Map<String,Object> updateTagBatchCode(BatchCode bean)throws Exception{
    	
    	List<BatchCodeItem> list = bean.getItems();
		List<FirstCodeItem> itemList = new ArrayList();
		for(BatchCodeItem item:list){
			FirstCodeItem firstCodeItem = new FirstCodeItem();
			firstCodeItem.setStarNum(item.getStartSeq());
			firstCodeItem.setRollNum(item.getVolumeNo());
			firstCodeItem.setEndNum(item.getEndSeq());
			firstCodeItem.setBatchCodeId(item.getBatchCodeId());
			firstCodeItem.setBeginCode(item.getStartVolumeCode());
			firstCodeItem.setEndCode(item.getEndVolumeCode());
			firstCodeItem.setRollNum(bean.getRollNum());
			itemList.add(firstCodeItem);			
		}
		FirstCode firstCode = new FirstCode();
		firstCode.setItems(itemList);
    	
    	Session session  = HibernateSessionFactory.getSession();
    	Connection conn = session.connection();
    	conn.setAutoCommit(false);
    	Boolean isUpdateSucc = true;
    	try {
    		dao.updateTagBatchCode(conn,firstCode.getItems());
    	    conn.commit();
       	    conn.setAutoCommit(true);

		} catch (Exception e) {
			isUpdateSucc = false;
			conn.rollback();
			throw e;
		}finally{
			this.close(conn);
			HibernateSessionFactory.closeSession();
		}
		return setMap(isUpdateSucc);
    }
    
    public Map setMap(boolean state){
        Map map = new HashMap();
        if(state){
            map.put("state", state);
            map.put("msg", "操作成功！");
        }else{
            map.put("state", state);
            map.put("msg", "操作失败！");
        }
        return map;   
    }
	@Override
	public GenericHibernateDao<FirstCode, Integer> getDao() {
		return dao;
	}
}
